package cn.qqncn.furniture.controller;

import cn.qqncn.furniture.mapper.OrderMapper;
import cn.qqncn.furniture.pojo.domain.Order;
import cn.qqncn.furniture.pojo.domain.User;
import cn.qqncn.furniture.pojo.vo.BarVo;
import cn.qqncn.furniture.pojo.vo.CarVo;
import cn.qqncn.furniture.service.CarService;
import cn.qqncn.furniture.service.OrderService;
import cn.qqncn.furniture.util.ResultUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 订单控制类
 *
 */
@CrossOrigin
@Controller
@RequestMapping("/order")
public class OrderController {
    @Resource
    OrderService orderService;

    @Resource
    CarService carService;

    ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 下单操作
     * @param request
     * @param order
     * @return
     */
    @PostMapping("addOrder")
    @ResponseBody
    public String addOrder(HttpServletRequest request,@RequestBody Order order) throws JsonProcessingException {
        System.out.println(request.getSession().getAttribute("user"));
        User user = (User) request.getSession().getAttribute("user");
        int i = orderService.addOrder(user.getUserId(), order);
        return ResultUtils.result(i);
    }

    @GetMapping("getBar")
    @ResponseBody
    public String getBar() throws JsonProcessingException {
        List<BarVo> barVos = orderService.selectBar();
        List<String> day = new ArrayList<>();
        List<Double> price = new ArrayList<>();
        for (BarVo barVo : barVos) {
            day.add(barVo.getDay());
            price.add(barVo.getPrice());
        }
        HashMap<String,List> map = new HashMap<>();
        map.put("days",day);
        map.put("price",price);
        return objectMapper.writeValueAsString(map);
    }

}
